<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 商品管理模块
 */
class Gift extends Admin_Controller {
	
	function __construct()
	{
	    parent::__construct();
            $this->load->model('gift_model');
            $this->load->model('cate_model');
	}
        //首页-溜溜币实物商品
	public function index()
	{
            $keyword = trim($_GET['q']);
            $like = $keyword ? $keyword : '';
            
            $where = array();
            $where['type'] = 3;//溜溜币实物类型
            
            $total = $this->gift_model->get_count($where,$like);
            $navigation_position = 'outside';
            $records_per_page = 10;
            $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position, array('left', 'right')) ? $navigation_position : 'outside');
            //$this->xpage->reverse(true);
            $this->xpage->records($total);
            $this->xpage->records_per_page($records_per_page);
            $this->xpage->labels("<<", ">>");
            $list = $this->gift_model->get_list($where, (($this->xpage->get_page() - 1) * $records_per_page) ,$records_per_page,$like);
            foreach ($list as $key => $val){
                $list[$key]['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
                $list[$key]['update_time'] = $val['update_time'] ? date('Y-m-d H:i:s',$val['update_time']) : '';
            }
            $this->template->assign('keyword',$like);
            $this->template->assign('Total',$total);
            $this->template->assign('showpage',$this->xpage->render(true));
            $this->template->assign('list',$list);
            $this->template->load('gift/index');
	}
        
        //溜溜币虚拟商品
        public function gift_virtual1(){
            $keyword = trim($_GET['q']);
            $like = $keyword ? $keyword : '';
            
            $where = array();
            $where['type'] = 4;//溜溜币虚拟类型
            $total = $this->gift_model->get_count($where,$like);
            $navigation_position = 'outside';
            $records_per_page = 10;
            $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position, array('left', 'right')) ? $navigation_position : 'outside');
            //$this->xpage->reverse(true);
            $this->xpage->records($total);
            $this->xpage->records_per_page($records_per_page);
            $this->xpage->labels("<<", ">>");
            $list = $this->gift_model->get_list($where, (($this->xpage->get_page() - 1) * $records_per_page) ,$records_per_page,$like);
            foreach ($list as $key => $val){
                $list[$key]['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
                $list[$key]['update_time'] = $val['update_time'] ? date('Y-m-d H:i:s',$val['update_time']) : '';
            }
            $this->template->assign('Total',$total);
            $this->template->assign('showpage',$this->xpage->render(true));
            $this->template->assign('list',$list);
            $this->template->assign('segment_p','index');
            $this->template->load('gift/gift_virtual1');
        }
        //添加商品
        function gift_add(){
            if($_POST){
                $mssage = array();
                $cate_id	= trim($_POST['cate_id']);
                $title		= trim($_POST['title']);
                $amount		= trim($_POST['amount']);
                $award_amount		= $_POST['award_amount'] ? trim($_POST['award_amount']) : 0;
                $thumb		= trim($_POST['thumb']);
                $description	= trim($_POST['description']);
                $sort               = trim($_POST['sort']);
                $status		= trim($_POST['status']);
                if(!$cate_id){
                        $mssage['info']	  = '请选择分类';
                        $mssage['status'] = 0;
                        $mssage['url']	  = '';
                        echo json_encode($mssage);exit;
                }
                
                $category = $this->cate_model->get_gift_data($cate_id);
                
                if(!$title){
                        $mssage['info']	  = '商品名称必须填写';
                        $mssage['status'] = 0;
                        $mssage['url']	  = '';
                        echo json_encode($mssage);exit;
                }
                $data = array();
                $data['title'] = $title;
                $data['cate_id'] = $cate_id;
                $data['amount'] = $amount;
                $data['award_amount'] = $award_amount;
                $data['thumb'] = $thumb;
                $data['description'] = $description;
                $data['status'] = $status;
                $data['sort'] = $sort;
                $data['type'] = $category['pid'];
                $data['admin_name'] = $this->session->userdata('username');
                $data['create_time'] = time();
                $result = $this->gift_model->insert($data);
                if($result){
                     $mssage['info']	  = '添加成功';
                     $mssage['status'] = 1;
                     $mssage['url']	  = $data['type'] == 3 ? site_url('gift/index') : site_url('gift/gift_virtual1');
                     echo json_encode($mssage);exit;
                }else{
                     $mssage['info']	  = '添加失败';
                     $mssage['status'] = 0;
                     $mssage['url']	  = $data['type'] == 3 ? site_url('gift/index') : site_url('gift/gift_virtual1');
                     echo json_encode($mssage);exit;
                }
            }
        $where = array();
        $where['group'] = 'currency';
        $catelist = $this->cate_model->get_gift_list($where);
        $catelist = $this->cate_model->tree($catelist);
        $this->template->assign('catelist',$catelist);
        $this->template->assign('segment_p','index');
        $this->template->load('gift/gift_add');
    }
    //编辑
    function gift_edit(){
        $id	   = intval($_GET['id']);
        if(!$id){
            redirect('gift/index');
        }
        if($_POST){
                    $mssage = array();
                    $id	= intval($_POST['id']);
                    $cate_id	= trim($_POST['cate_id']);
                    $title		= trim($_POST['title']);
                    $amount		= trim($_POST['amount']);
                    $award_amount		= $_POST['award_amount'] ? trim($_POST['award_amount']) : 0;
                    $sort               = trim($_POST['sort']);
                    if(trim($_REQUEST['thumb'])){
                        $thumb = trim($_REQUEST['thumb']);
                    }
                    $description	= trim($_POST['description']);
                    $status		= trim($_POST['status']);
                    if(!$cate_id){
                            $mssage['info']	  = '请选择分类';
                            $mssage['status'] = 0;
                            $mssage['url']	  = '';
                            echo json_encode($mssage);exit;
                    }
                    $category = $this->cate_model->get_gift_data($cate_id);
                    if(!$title){
                            $mssage['info']	  = '商品名称必须填写';
                            $mssage['status'] = 0;
                            $mssage['url']	  = '';
                            echo json_encode($mssage);exit;
                    }
                    $data = array();
                    $data['cate_id'] = $cate_id;
                    $data['title'] = $title;
                    if(!empty($thumb)){
                        $data['thumb'] = $thumb;
                    }
                    $data['description'] = $description;
                    $data['status'] = $status;
                    $data['amount'] = $amount;
                    $data['type'] = $category['pid'];
                    $data['award_amount'] = $award_amount;
                    $data['sort'] = $sort;
                    $data['admin_name'] = $this->session->userdata('username');
                    $data['update_time'] = time();
                    $result = $this->gift_model->update($data,$id);
                    if($result){
                         $mssage['info']	  = '修改成功';
                         $mssage['status'] = 1;
                         $mssage['url']	  = $data['type'] == 3 ? site_url('gift/index') : site_url('gift/gift_virtual1');
                         echo json_encode($mssage);exit;
                    }else{
                         $mssage['info']	  = '修改失败';
                         $mssage['status'] = 0;
                         $mssage['url']	  = $data['type'] == 3 ? site_url('gift/index') : site_url('gift/gift_virtual1');
                         echo json_encode($mssage);exit;
                    }
		}
            $item = $this->gift_model->Get($id);
            $where = array();
            $where['group'] = 'currency';
            $catelist = $this->cate_model->get_gift_list($where);
            $catelist = $this->cate_model->tree($catelist);
            $this->template->assign('catelist',$catelist);
            $this->template->assign('segment_p','index');
            $this->template->assign('item',$item);
            $this->template->load('gift/gift_edit');
        }
        
        
        //删除
        function gift_remove(){
            $id	   = intval($_GET['id']);
            if(!$id){
                redirect('gift/index');
            }
            
            $item = $this->gift_model->Get($id);
            $result = $this->gift_model->dalete($id);
            if($this->db->affected_rows()){
                    $mssage['info']	  = '删除成功';
                        $mssage['status'] = 1;
                        $mssage['url']	  = $item['type'] == 3 ? site_url('gift/index') : site_url('gift/gift_virtual1');
                        echo json_encode($mssage);exit;
           }else{
                    $mssage['info']	  = '删除失败';
                        $mssage['status'] = 0;
                        $mssage['url']	  = $item['type'] == 3 ? site_url('gift/index') : site_url('gift/gift_virtual1');
                        echo json_encode($mssage);exit;
           }
        }
        
        //设置显示状态
        function gift_setstatus(){
            $id	   = intval($_GET['ids']);
            if(!$id){
                redirect('gift/index');
            }
            $item = $this->gift_model->Get($id);
            $status = trim($_GET['status']);
            $result = $this->gift_model->setstatus($status,$id);
            if($this->db->affected_rows()){
                $mssage['info']	  = '修改成功';
                $mssage['status'] = 1;
                $mssage['url']	  = $item['type'] == 3 ? site_url('gift/index') : site_url('gift/gift_virtual1');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '修改失败';
                $mssage['status'] = 0;
                $mssage['url']	  = $item['type'] == 3 ? site_url('gift/index') : site_url('gift/gift_virtual1');
                echo json_encode($mssage);exit;
            }
        }
        
        function gift_setindex(){
            $id	   = intval($_GET['ids']);
            if(!$id){
                redirect('gift/index');
            }
            $item = $this->gift_model->Get($id);
            $is_index = trim($_GET['index']);
            $result = $this->gift_model->setindex($is_index,$id);
            if($this->db->affected_rows()){
                $mssage['info']	  = '修改成功';
                $mssage['status'] = 1;
                $mssage['url']	  = $item['type'] == 3 ? site_url('gift/index') : site_url('gift/gift_virtual1');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '修改失败';
                $mssage['status'] = 0;
                $mssage['url']	  = $item['type'] == 3 ? site_url('gift/index') : site_url('gift/gift_virtual1');
                echo json_encode($mssage);exit;
            }
        }
}
